package com.yulongtian.oneProblemEveryDay.month202302;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts/
 * 可
 *
 * @author yulongTian
 * @create 2023-02-24 11:03
 */
public class Test20 {
    public static void main(String[] args) {
        int[] nums = {1, 5, 0, 3, 5};
        System.out.println(minimumOperations(nums));

    }

    public static int minimumOperations(int[] nums) {
        int ans = 0;
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });

        for (int num : nums) {
            if (num != 0) {
                priorityQueue.add(num);
            }
        }
        while (!priorityQueue.isEmpty()) {
            Integer min = priorityQueue.poll();
            int len = priorityQueue.size();
            List<Integer> temp_list = new ArrayList<>(len);
            for (int i = 0; i < len; i++) {
                int temp = priorityQueue.poll() - min;
                if (temp != 0) {
                    temp_list.add(temp);
                }
            }
            for (Integer integer : temp_list) {
                priorityQueue.add(integer);
            }
            ans++;
        }
        return ans;
    }
}
